﻿using IOP.ISEA;
using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading.Tasks;

namespace VkSample66
{
    class Program
    {
        static async Task Main(string[] args)
        {
            CreateISEA(true);
            var h = Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) config.AddVulkanJsonFile("Vulkan.json");
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) config.AddVulkanJsonFile("Vulkan-Linux.json");
                    else throw new NotSupportedException("Not supported Platform");
                })
                .ConfigureSgrAHost((sgra) =>
                {
                    sgra.AddVulkanEnvironmental();
                    sgra.UseStartup<Startup>();
                }).ConfigureLogging((logger) => logger.AddConsole()); ;
            await h.RunConsoleAsync();
        }

        static void CreateISEA(bool isNeed)
        {
            var texPath = Path.Combine(AppContext.BaseDirectory, "Assets", "Textures");
            var imageJson = JsonDocument.Parse(File.ReadAllText(Path.Combine(texPath, "texture.json")));
            var textureJson = imageJson.RootElement.GetProperty("Texture").GetRawText();
            var samplerJson = imageJson.RootElement.GetProperty("Sampler").GetRawText();
            ISEADocument document = new ISEADocument();
            ConfigNode textureConfig = document.CreateConfigNode("TexConfig", textureJson, ConfigEngine.Vulkan);
            ConfigNode sampler = document.CreateConfigNode("Sampler", samplerJson, ConfigEngine.Vulkan);
            IOP.ISEA.ImageNode earth = document.CreateImageNode("EarthImage", Path.Combine(texPath, "earth.bntex"));
            IOP.ISEA.ImageNode earthn = document.CreateImageNode("EarthNImage", Path.Combine(texPath, "earthn.bntex"));
            IOP.ISEA.ImageNode moon = document.CreateImageNode("MoonImage", Path.Combine(texPath, "moon.bntex"));
            BallData ballData = new BallData();
            SkyData bigSkyData = new SkyData();
            SkyData smallSkyData = new SkyData();
            ballData.GenBallData(3);
            bigSkyData.GenSkyData(400);
            smallSkyData.GenSkyData(1600);
            MeshNode ball = document.CreateEmptyMeshNode($"BallMesh", (int)ballData.VCount)
                .AddMeshData(ballData.Data, 20, 0, MeshDataPurpose.COMPLEX, false);
            MeshNode bigShy = document.CreateEmptyMeshNode($"BigShyMesh", (int)bigSkyData.VCount)
                .AddMeshData(bigSkyData.Data, 20, 0, MeshDataPurpose.COMPLEX, false);
            MeshNode samllSky = document.CreateEmptyMeshNode($"SmallSkyMesh", (int)smallSkyData.VCount)
                .AddMeshData(smallSkyData.Data, 20, 0, MeshDataPurpose.COMPLEX, false);
            TextureNode earthTex = document.CreateTextureNode($"EarthTex", TextureUsage.CombinedImageSampler, earth,
                textureConfig, sampler, 1);
            TextureNode earthNTex = document.CreateTextureNode($"EarthNTex", TextureUsage.CombinedImageSampler, earthn,
                textureConfig, sampler, 1, 1);
            TextureNode moonTex = document.CreateTextureNode($"MoonTex", TextureUsage.CombinedImageSampler, moon,
                textureConfig, sampler, 1);
            document.CreateRenderObjectNode($"Earth", ball, earthTex, earthNTex);
            document.CreateRenderObjectNode($"Moon", ball, moonTex);
            document.CreateRenderObjectNode($"BigSky", bigShy);
            document.CreateRenderObjectNode($"SmallSky", samllSky);
            document.Save(AppContext.BaseDirectory, "Test66");
        }
    }
}
